mod lcseq;

use std::any::Any;
use std::fs;
use std::str::FromStr;
use rsip;
use rsip::{Auth, Header, Headers, Method, SipMessage};
use rsip::message::{HasHeaders, HeadersExt};
use crate::lipo_info;

pub(crate) fn test_sip() {
    // lipo_info!(format!("\n~~~~~~~~~~~~~~~~~\n {}\n~~~~~~~~~~~~~~~~~\n",generate_register_request1()));

    parse_sip();
}


fn parse_sip() {
    let contents = fs::read_to_string(r"D:\it\cache\test\sip.txt")
        .expect("无法读取数据");
    let sipmessage = SipMessage::try_from(contents).expect("解析失败");

    println!("{:?}", sipmessage);
   if sipmessage.cseq_header().unwrap().method().unwrap() == Method::Register {
       println!("是的");
   }
    // 循环查询 header
    for header in sipmessage.headers().iter() {
        match header {
            Header::Other(key, value) => {
                if "X-Vowifi-Pusha" == key {
                    println!("value: {}", value);
                }
            }
            _ => {}
        }
    }
}


fn generate_register_request1() -> String {
    let mut headers: rsip::Headers = Default::default();


    // for header in headers {
    //     match header {
    //         Header::CallId(id) => {
    //
    //         }
    //         _ => {
    //
    //         }
    //     }
    // }


    let base_uri = rsip::Uri {
        scheme: Some(rsip::Scheme::Sip),
        auth: Some(("+8613198349816", Option::<String>::None).into()),
        host_with_port: rsip::Domain::from("sc.vowifi.ims.mnc001.mcc460.3gppnetwork.org").into(),
        ..Default::default()
    };

    //  添加 from
    headers.push(rsip::typed::From {
        display_name: None,
        uri: base_uri,
        params: vec![rsip::Param::Tag(rsip::param::Tag::new("G2GuOMG7HUC9ahGaNuy321*t4-"))],
    }.into());

    headers.push(Header::Other("X-Vowifi-Pusha".to_string(), "com.citc.rcs.full/4".to_string()));
    headers.push(Header::Other("X-Vowifi-Datal".to_string(), "34.77221-113.72818".to_string()));

    return rsip::Request {
        method: rsip::Method::Register,
        uri: rsip::Uri {
            scheme: Some(rsip::Scheme::Sip),
            auth: Some(("", Option::<String>::None).into()),
            host_with_port: rsip::Domain::from("ims.mnc001.mcc460.3gppnetwork.org").into(),
            ..Default::default()
        },
        version: rsip::Version::V2,
        headers,
        body: Default::default(),
    }.into();
}

fn generate_register_request() -> rsip::SipMessage {
    let mut headers: rsip::Headers = Default::default();

    let base_uri = rsip::Uri {
        scheme: Some(rsip::Scheme::Sips),
        auth: Some(("bob", Option::<String>::None).into()),
        host_with_port: rsip::Domain::from("biloxi.example.com").into(),
        ..Default::default()
    };

    headers.push(
        rsip::typed::Via {
            version: rsip::Version::V2,
            transport: rsip::Transport::Tls,
            uri: rsip::Uri {
                host_with_port: (rsip::Domain::from("client.biloxi.example.com"), 5060).into(),
                ..Default::default()
            },
            params: vec![rsip::Param::Branch(rsip::param::Branch::new(
                "z9hG4bKnashds7",
            ))],
        }
            .into(),
    );
    headers.push(rsip::headers::MaxForwards::default().into());
    headers.push(
        rsip::typed::From {
            display_name: Some("Bob".into()),
            uri: base_uri.clone(),
            params: vec![rsip::Param::Tag(rsip::param::Tag::new("a73kszlfl"))],
        }
            .into(),
    );
    headers.push(
        rsip::typed::To {
            display_name: Some("Bob".into()),
            uri: base_uri.clone(),
            params: Default::default(),
        }
            .into(),
    );
    headers.push(rsip::headers::CallId::default().into());
    headers.push(
        rsip::typed::CSeq {
            seq: 1,
            method: rsip::Method::Register,
        }
            .into(),
    );
    headers.push(
        rsip::typed::Contact {
            display_name: None,
            uri: base_uri,
            params: Default::default(),
        }
            .into(),
    );
    headers.push(rsip::headers::ContentLength::default().into());

    rsip::Request {
        method: rsip::Method::Register,
        uri: rsip::Uri {
            scheme: Some(rsip::Scheme::Sips),
            host_with_port: rsip::Domain::from("ss2.biloxi.example.com").into(),
            ..Default::default()
        },
        version: rsip::Version::V2,
        headers,
        body: Default::default(),
    }.into()
}